package com.spider.silence.main;

import com.spider.silence.downloader.TianFuForumDownLoader;
import com.spider.silence.pool.TianFuForumDownLoaderPool;
import com.spider.silence.utils.PropertiesUtil;
import com.spider.silence.worker.TianFuForumModuleWorker;
import com.spider.silence.worker.TianFuForumWorker;
import org.apache.log4j.PropertyConfigurator;

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

/**
 * Created by Administrator on 2016/12/28 0028.
 */
public class TianFuForumMain {

    public static void main(String[] args) throws  Exception{


        //加载日志环境
        PropertyConfigurator.configure( PropertiesUtil.getLogPro());

        //初始化天府新区下载池
        TianFuForumDownLoaderPool downLoaderPool = TianFuForumDownLoaderPool.getInstance();
        //初始化任务队列
        TianFuForumDownLoader downLoader = (TianFuForumDownLoader) downLoaderPool.fetchDownLoader(1000);
        List<String> urlList = downLoader.getUrl();
        //生成任务队列
        downLoader.generateTask(urlList);
        //放回到池中
        downLoaderPool.releaseDownLoader(downLoader);
        //初始化Worker
        ExecutorService pool =  Executors.newFixedThreadPool(Integer.valueOf(PropertiesUtil.getVal("main.woker.size")));
        for(int i=0;i<Integer.valueOf(PropertiesUtil.getVal("TianFuForum.WorkerCount"));i++){
            pool.execute(new TianFuForumModuleWorker());
            pool.execute(new TianFuForumWorker());
        }

//        TianFuForumDownLoaderPool downLoaderPool = null;
//        TianFuForumDownLoader downLoader = null;
//        try{
//            //初始化天府新区下载池
//            downLoaderPool = TianFuForumDownLoaderPool.getInstance();
//            //初始化任务队列
//            downLoader = (TianFuForumDownLoader) downLoaderPool.fetchDownLoader(1000);
//
//            List<String> urlList = downLoader.getUrl();
//            //生成任务队列
//            downLoader.generateTask(urlList);
//        }catch (Exception e){
//            e.printStackTrace();
//        }finally{
//            if(downLoader !=null) {
//                downLoaderPool.releaseDownLoader(downLoader);
//            }
//        }
    }

}
